/**
 * 项目名：admin
 * 包名：com.xuebang.admin.controller
 * 文件名：ColumnController.java
 * 版本信息：@version 1.0
 * 日期：2014-2-11-下午1:25:48
 */
package com.xuebang.admin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuebang.admin.dao.Pagination;
import com.xuebang.admin.entity.OperatorRules;
import com.xuebang.admin.entity.Suggestion;
import com.xuebang.admin.service.SuggestionService;
import com.xuebang.admin.util.JsonUtil;
import com.xuebang.admin.web.Result;

/**
 * 项目名称：admin
 * 类名称：SuggestionController
 * 类描述：
 * 创建人：liulina
 * 创建时间：2016-2-29 下午17:08:48
 * 修改人：
 * 修改时间：
 * 修改备注：
 * @version 
 */
@Controller
@RequestMapping("/suggestion")
public class SuggestionController extends BaseController {
	
	
	/** KindEditor 分页符 */
	private String strPageBreak = "<hr style=\"page-break-after:always;\" class=\"ke-pagebreak\" />";

	/** 页码 */
	Integer intPageNo = 1;
	/** 页面条数  */
	Integer intPageLimit = 10;
	
	/** 提问业务逻辑类  */
	@Inject 
	SuggestionService suggestionService;

	List<Suggestion> lstSuggestions = new ArrayList<Suggestion>();
	/**
	 * 展示所有 
	 */
	@RequestMapping(value="/operate")
	public String operate(){
		
		if(!this.hasPerm(OperatorRules.PERM_SUGGEST)){
			return this.no_perm_page();
		}

		return "/suggestion/operate";
	}
	
	/**
	 * 展示所有
	 */
	@RequestMapping(value="/showSuggestions")
	public String showSuggestions(Model model,String pageNo){
		
		Pagination<Suggestion> lstSuggestions = suggestionService.getSuggestionInfo("",1,intPageLimit);

		model.addAttribute("lstSuggestions", lstSuggestions);
		model.addAttribute("pageSum", lstSuggestions.totalPages());
		return "/suggestion/showSuggestions";
	}

	/**
	 * 跳转到增加新栏目页面
	 */
	@RequestMapping(value="/update")
	public String update(Integer intSuggestionID,Model model){

		if(!this.hasPerm(OperatorRules.PERM_SUGGEST)){
			return this.no_perm_page();
		}
		Suggestion suggestion = this.suggestionService.loadSuggestion(intSuggestionID);

		model.addAttribute("suggestion",suggestion);
		return "/suggestion/update";
	}
	

	/**
	 * 更新
	 * 
	 * @param 表单数据
	 */
	@RequestMapping(value="/toUpdate",method = RequestMethod.POST,produces = "text/plain;charset=UTF-8")
	public String toUpdate(@Valid Suggestion suggestion,HttpServletRequest request,Model model){

		try{
			suggestion.setSign(1);
			this.suggestionService.updatesuggestion(suggestion);
			model.addAttribute("resultMessage","更新成功");
			model.addAttribute("resultType",1);
		} catch(Exception e){
			model.addAttribute("resultMessage","更新失败");
			model.addAttribute("resultType",0);
		}
	
		return "/suggestion/update";
	}

	
	/**
	 * 删除
	 * 
	 * @param 表单数据
	 */
	@RequestMapping(value="/toDelete",method = RequestMethod.POST,produces = "text/plain;charset=UTF-8")
	public @ResponseBody String toDelete(String strSuggestion){

		if(!this.hasPerm(OperatorRules.PERM_SUGGEST)){
			return this.no_perm_page();
		}
		if(strSuggestion!=null && !"".equals(strSuggestion.trim())) {
			try{
				// 去掉 开始和结尾的,
				if(strSuggestion.length()>0){
					if(strSuggestion.charAt(0)==','){
						strSuggestion = strSuggestion.substring(1, strSuggestion.length());
					}
					if(strSuggestion.length()>0){
						if(strSuggestion.charAt(strSuggestion.length()-1)==','){
							strSuggestion = strSuggestion.substring(0, strSuggestion.length()-1);
						}
						this.suggestionService.deletesuggestions(strSuggestion);
						return JsonUtil.dump(Result.success("删除成功"));
					} else {
						return JsonUtil.dump(Result.error("删除失败"));
					}
				} else {
					return JsonUtil.dump(Result.error("删除失败"));
				}
			} catch(Exception e){
				e.printStackTrace();
				return JsonUtil.dump(Result.error("删除失败"));
			}
		} else {
			return JsonUtil.dump(Result.error("删除失败"));
		}
	}
	

	/**
	 * 搜索 
	 */
	@RequestMapping(value="searchSuggestion",method=RequestMethod.GET,produces = "text/plain;charset=UTF-8")
	public String searchSuggestion(String searchText,String pageNo,Model model){
		try{
			intPageNo = Integer.parseInt(pageNo);
		} catch(Exception e){
			e.printStackTrace();
		}
		Pagination<Suggestion> lstSuggestions = null;
		if(searchText!=null &&!"".equals(searchText.trim())){
			lstSuggestions = this.suggestionService.searchSuggestion(searchText.trim(),intPageNo,intPageLimit);
			model.addAttribute("lstSuggestions", lstSuggestions);
			model.addAttribute("pageSum", lstSuggestions.totalPages());
		} else {
			lstSuggestions = this.suggestionService.searchSuggestion("",intPageNo,intPageLimit);
			model.addAttribute("lstSuggestions", lstSuggestions);
			model.addAttribute("pageSum", lstSuggestions.totalPages());
		}
		return "/suggestion/showSuggestions";
	}
	
	
	@RequestMapping(value="/filterSuggestion",method = RequestMethod.GET,produces = "text/plain;charset=UTF-8")
	public String filterSuggestion(String searchText,Integer pageNo,Model model){

		if(!this.hasPerm(OperatorRules.PERM_SUGGEST)){
			return this.no_perm_page();
		}

		if(pageNo == null || pageNo < 1){
			pageNo = 1;
		}
		
		Pagination<Suggestion> lstSuggestion=null;
			lstSuggestion =  suggestionService.getSuggestionInfo(searchText,pageNo,intPageLimit);
		
		model.addAttribute("lstSuggestion", lstSuggestion);
		model.addAttribute("pageSum", lstSuggestion.totalPages());

		return "/suggestion/showSuggestions";
	}
}
